package com.zsc.wxm.ysh.anjo.member.aop;

import cn.hutool.core.collection.CollUtil;
import com.zsc.wxm.ysh.anjo.member.model.Member;
import com.zsc.wxm.ysh.anjo.member.mapper.MemberMapper;
import com.zsc.wxm.ysh.anjo.member.service.LoginLogService;
import com.zsc.wxm.ysh.anjo.service.base.model.member.qo.MemberQuery;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/10 20:46
 */
@Aspect
@Component
@Slf4j
public class LoginAspect {

    @Resource
    private LoginLogService loginLogService;

    @Resource
    private MemberMapper memberMapper;

    /**
     * 切入点
     */
    @Pointcut("@annotation(com.zsc.wxm.ysh.anjo.service.base.annotations.LoggerHandler)")
    public void aspectPoint() {

    }

    @After("aspectPoint()")
    public void loginLogAspect(JoinPoint joinPoint) {
        log.info("调用登录切面....");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object[] args = joinPoint.getArgs();
        String phone = (String) args[0];
        List<Member> list = memberMapper.listByQo(MemberQuery.builder().phone(phone).build());
        if (CollUtil.isNotEmpty(list)) {
            loginLogService.saveLoginLog(request, list.get(0));
        }
    }
}
